--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
meshchatx/src/backend/async_utils.py 0d540279c0c2180d98f8d741894d2f68c807477f (0d540279) Text, 2.48 KB
Tff7b72import T7ee787asyncio
Tff7b72import T7ee787sys
Tff7b72import T7ee787threading
Tff7b72from T7ee787collectionsT7ee787.T7ee787abc Tff7b72import Te6edf3Coroutine
Tff7b72class T56d364AsyncUtilsTb4b4b4:
Te6edf3main_loopTb4b4b4: Te6edf3asyncioTff7b72.Td2a8ffAbstractEventLoop Tff7b72| Tff7b72None Tff7b72= Tff7b72None
Te6edf3_pending_futuresTb4b4b4: Tffa657list Tff7b72= Tb4b4b4[Tb4b4b4]
Te6edf3_futures_lock Tff7b72= Te6edf3threadingTff7b72.Td2a8ffLockTb4b4b4(Tb4b4b4)
Te6edf3_FUTURES_SWEEP_THRESHOLD Tff7b72= T79c0ff64
Tf0883e@staticmethod
Tff7b72def Td2a8ffapply_asyncio_313_patchTb4b4b4(Tb4b4b4)Tb4b4b4:
T8b949e"""Apply a patch for asyncio on Python 3.13 to avoid a bug in sendfile with SSL.
See: https://github.com/python/cpython/issues/124448
And: https://github.com/aio-libs/aiohttp/issues/8863
"""
Tff7b72if Te6edf3sysTff7b72.Td2a8ffversion_info Tff7b72>Tff7b72= Tb4b4b4(T79c0ff3Tb4b4b4, T79c0ff13Tb4b4b4)Tb4b4b4:
Tff7b72import T7ee787asyncioT7ee787.T7ee787base_events
Te6edf3original_sendfile Tff7b72= Te6edf3asyncioTff7b72.Td2a8ffbase_eventsTff7b72.Td2a8ffBaseEventLoopTff7b72.Td2a8ffsendfile
Tff7b72async Tff7b72def Td2a8ffpatched_sendfileTb4b4b4(
Tff7b72selfTb4b4b4,
Te6edf3transportTb4b4b4,
Te6edf3fileTb4b4b4,
Te6edf3offsetTff7b72=T79c0ff0Tb4b4b4,
Te6edf3countTff7b72=Tff7b72NoneTb4b4b4,
Tff7b72*Tb4b4b4,
Te6edf3fallbackTff7b72=Tff7b72TrueTb4b4b4,
Tb4b4b4)Tb4b4b4:
Tff7b72if Te6edf3transportTff7b72.Td2a8ffget_extra_infoTb4b4b4(Ta5d6ff"Ta5d6ffsslcontextTa5d6ff"Tb4b4b4)Tb4b4b4:
Tff7b72raise Tf85149NotImplementedErrorTb4b4b4(
Ta5d6ff"Ta5d6ffsendfile is broken on SSL transports in Python 3.13Ta5d6ff"Tb4b4b4,
Tb4b4b4)
Tff7b72return Tff7b72await Te6edf3original_sendfileTb4b4b4(
Tff7b72selfTb4b4b4,
Te6edf3transportTb4b4b4,
Te6edf3fileTb4b4b4,
Te6edf3offsetTb4b4b4,
Te6edf3countTb4b4b4,
Te6edf3fallbackTff7b72=Te6edf3fallbackTb4b4b4,
Tb4b4b4)
Te6edf3asyncioTff7b72.Td2a8ffbase_eventsTff7b72.Td2a8ffBaseEventLoopTff7b72.Td2a8ffsendfile Tff7b72= Te6edf3patched_sendfile
Tf0883e@staticmethod
Tff7b72def Td2a8ffset_main_loopTb4b4b4(Te6edf3loopTb4b4b4: Te6edf3asyncioTff7b72.Td2a8ffAbstractEventLoopTb4b4b4)Tb4b4b4:
Te6edf3AsyncUtilsTff7b72.Td2a8ffmain_loop Tff7b72= Te6edf3loop
Tf0883e@staticmethod
Tff7b72def Td2a8ffrun_asyncTb4b4b4(Te6edf3coroutineTb4b4b4: Te6edf3CoroutineTb4b4b4)Tb4b4b4:
T8b949e"""Schedule *coroutine* on the main event loop from any thread.
Returned futures are tracked so they (and the closures they reference)
can be garbage-collected promptly once finished.
"""
Tff7b72if Te6edf3AsyncUtilsTff7b72.Td2a8ffmain_loop Tff7b72and Te6edf3AsyncUtilsTff7b72.Td2a8ffmain_loopTff7b72.Td2a8ffis_runningTb4b4b4(Tb4b4b4)Tb4b4b4:
Te6edf3future Tff7b72= Te6edf3asyncioTff7b72.Td2a8ffrun_coroutine_threadsafeTb4b4b4(
Te6edf3coroutineTb4b4b4, Te6edf3AsyncUtilsTff7b72.Td2a8ffmain_loopTb4b4b4,
Tb4b4b4)
Tff7b72with Te6edf3AsyncUtilsTff7b72.Td2a8ff_futures_lockTb4b4b4:
Te6edf3AsyncUtilsTff7b72.Td2a8ff_pending_futuresTff7b72.Td2a8ffappendTb4b4b4(Te6edf3futureTb4b4b4)
Tff7b72if Tffa657lenTb4b4b4(Te6edf3AsyncUtilsTff7b72.Td2a8ff_pending_futuresTb4b4b4) Tff7b72>Tff7b72= Te6edf3AsyncUtilsTff7b72.Td2a8ff_FUTURES_SWEEP_THRESHOLDTb4b4b4:
Te6edf3AsyncUtilsTff7b72.Td2a8ff_pending_futures Tff7b72= Tb4b4b4[
Te6edf3f Tff7b72for Te6edf3f Tff7b72in Te6edf3AsyncUtilsTff7b72.Td2a8ff_pending_futures Tff7b72if Tff7b72not Te6edf3fTff7b72.Td2a8ffdoneTb4b4b4(Tb4b4b4)
Tb4b4b4]
Tff7b72return
Tffa657printTb4b4b4(Ta5d6ff"Ta5d6ffWARNING: Main event loop not available. Could not schedule task.Ta5d6ff"Tb4b4b4)
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────